//
//  ForgetPasswordViewController.m
//  DA_V
//
//  Created by 周季阳 on 15/4/22.
//  Copyright (c) 2015年 zhouhai. All rights reserved.
//

#import "ForgetPasswordViewController.h"
#import "InterFace.h"
#import "UIViewAdditions.h"
#import "UpdatePasswordViewController.h"
#import "LoginData.h"
#import "JudgePhoneNumber.h"
@interface ForgetPasswordViewController ()<UITextFieldDelegate,LoginDelegate>
{
    UITextField *writePhoneNum;
    UITextField *writePhoneNum1;
}
@end

@implementation ForgetPasswordViewController
@synthesize verifyStr;
#pragma mark - viewDidLoad
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [self colorWithHexString:@"#F5F5F5"];
    [self initNav];
    [self initUI];
}
#pragma mark - initUI
-(void)initUI{
    
    UIView *backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, navView.bottom, SCREEN_WIDTH, SCREEN_HEIGHT*0.6)];
    backgroundView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:backgroundView];
    
    UIImageView *loginPwdBG = [[UIImageView alloc] initWithFrame:CGRectMake(40,100, SCREEN_WIDTH-80,  SCREEN_WIDTH*0.12)];
    // loginPwdBG.image = [UIImage imageNamed:@"updatepassword_03"];
    loginPwdBG.layer.borderColor = [UIColor grayColor].CGColor;
    loginPwdBG.layer.borderWidth = 0.5;
    loginPwdBG.backgroundColor = [self colorWithHexString:@"#FFFFFF"];
    loginPwdBG.userInteractionEnabled = YES;
    [backgroundView addSubview:loginPwdBG];
    
    
    writePhoneNum = [[UITextField alloc] initWithFrame:CGRectMake(10, 0 ,loginPwdBG.frame.size.width-20 , SCREEN_WIDTH*0.12)];
    writePhoneNum.tag = 4;
    writePhoneNum.font = [UIFont systemFontOfSize:14];
    writePhoneNum.placeholder = @"输入账号";
    //writePhoneNum.backgroundColor= [UIColor whiteColor];
    [loginPwdBG addSubview:writePhoneNum];
    writePhoneNum.delegate = self;
    
    UIImageView *loginPwdBG1 = [[UIImageView alloc] initWithFrame:CGRectMake(40,loginPwdBG.bottom+14, SCREEN_WIDTH/2-40, SCREEN_WIDTH*0.12)];
    loginPwdBG1.layer.borderColor = [UIColor grayColor].CGColor;
    loginPwdBG1.layer.borderWidth = 0.5;
    //loginPwdBG1.image = [UIImage imageNamed:@"forgetpassword_06"];
    loginPwdBG1.backgroundColor = [self colorWithHexString:@"#FFFFFF"];
    loginPwdBG1.userInteractionEnabled = YES;
    [backgroundView addSubview:loginPwdBG1];
    
    
    writePhoneNum1 = [[UITextField alloc] initWithFrame:CGRectMake(10, 0 ,loginPwdBG1.frame.size.width-20 , SCREEN_WIDTH*0.12)];
    writePhoneNum1.tag = 5;
    writePhoneNum1.placeholder = @"输入验证码";
    writePhoneNum1.font = [UIFont systemFontOfSize:14];
    //writePhoneNum.backgroundColor= [UIColor whiteColor];
    [loginPwdBG1 addSubview:writePhoneNum1];
    writePhoneNum1.delegate = self;
    
    
    UIButton *sendCode = [UIButton buttonWithType:UIButtonTypeCustom];
    //[sendCode setTitle:@"发送验证码" forState:UIControlStateNormal];
    sendCode.tag = 100;
    sendCode.frame = CGRectMake(loginPwdBG1.right+5, loginPwdBG.bottom+10, SCREEN_WIDTH/2-40, SCREEN_WIDTH*0.14);
    [sendCode setBackgroundImage:[UIImage imageNamed:@"sendnum"] forState:UIControlStateNormal];
    [sendCode setBackgroundImage:[UIImage imageNamed:@"sendnum1"] forState:UIControlStateSelected];
    sendCode.selected = NO;
    [sendCode addTarget:self action:@selector(clickYanZheng:) forControlEvents:UIControlEventTouchUpInside];
    [backgroundView addSubview:sendCode];
    
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(20, sendCode.bottom+80, SCREEN_WIDTH/2-30, SCREEN_WIDTH*0.11);
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"sure"] forState:UIControlStateNormal];
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"sure1"] forState:UIControlStateHighlighted];
    [leftBtn addTarget:self action:@selector(updatePassword) forControlEvents:UIControlEventTouchUpInside];
    [backgroundView addSubview:leftBtn];
    
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(leftBtn.right+20, sendCode.bottom+80, SCREEN_WIDTH/2-30, SCREEN_WIDTH*0.11);
    [rightBtn setBackgroundImage:[UIImage imageNamed:@"cancel"] forState:UIControlStateNormal];
    [rightBtn setBackgroundImage:[UIImage imageNamed:@"cancel1"] forState:UIControlStateHighlighted];
    [rightBtn addTarget:self action:@selector(clickRightBtn) forControlEvents:UIControlEventTouchUpInside];
    [backgroundView addSubview:rightBtn];
    
    UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap)];
    [singleTapGestureRecognizer setNumberOfTapsRequired:1];
    [backgroundView addGestureRecognizer:singleTapGestureRecognizer];
    
}
-(void)singleTap{
    [writePhoneNum resignFirstResponder];
    [writePhoneNum1 resignFirstResponder];
}
#pragma mark - UItextField Delegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
#pragma mark - 发送验证码
static int i = 60;
-(void)clickYanZheng:(UIButton *)sender{
    if (writePhoneNum.text.length < 1) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"请先输入手机号" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }else{
    UIButton *btn = (UIButton *)[self.view viewWithTag:100];
    btn.selected = YES;
    if (btnView) {
        
    }else{
        UITextField *text = (UITextField *)[self.view viewWithTag:4];
        NSString *udid = [[NSUserDefaults standardUserDefaults] objectForKey:@"UDIDStr"];
        if ([JudgePhoneNumber isMobileNumberClassification:text.text]) {
            LoginData *login = [[LoginData alloc]init];
            [login sendMessageNumber:text.text andServiceID:udid];
            login.delegate = self;
            zTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
            btnView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH*0.375, SCREEN_WIDTH*0.14)];
            btnView.userInteractionEnabled = YES;
            btnView.backgroundColor = [UIColor clearColor];
            [btn addSubview:btnView];
            
            zBtnLabel = [[UILabel alloc]initWithFrame:CGRectMake(0.0625*SCREEN_WIDTH, 0 , 30, SCREEN_WIDTH*0.14)];
            zBtnLabel.font = [UIFont boldSystemFontOfSize:15];
            zBtnLabel.textColor = [UIColor redColor];
            zBtnLabel.text = @"60";
            [btn addSubview:zBtnLabel];
            i = 60;
            UIButton *sendCodeButton = (UIButton *)[self.view viewWithTag:100];
            [sendCodeButton setBackgroundImage:[UIImage imageNamed:@"sendnum1"] forState:UIControlStateSelected];
        }else{
            UIAlertView *mobileNumberAlert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"手机号码不正确,请检查后重新输入" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [mobileNumberAlert show];
            UIButton *sendCodeButton = (UIButton *)[self.view viewWithTag:100];
            [sendCodeButton setBackgroundImage:[UIImage imageNamed:@"sendnum"] forState:UIControlStateSelected];
        }
    }
    }
}
#pragma mark - 短信验证回调
-(void)sendMessage:(NSDictionary *)sign{
    if ([[sign objectForKey:@"status"]isEqualToString:@"success"]) {
    //验证码
    verifyStr =[[sign objectForKey:@"data"] objectForKey:@"CodeID"];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[sign objectForKey:@"reason"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}
#pragma mark - 短信验证回调失败
-(void)requistFail{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"网络异常,无法加载数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}
-(void)updateTime{
    i--;
    zBtnLabel.text = [NSString stringWithFormat:@"%d",i];
    if (i == 0) {
        [zTimer invalidate];
        zTimer = nil;
        UIButton *btn = (UIButton *)[self.view viewWithTag:100];
        btn.selected = NO;
        [btnView removeFromSuperview];
        btnView = nil;
        [zBtnLabel removeFromSuperview];
        zBtnLabel = nil;
    }
}
#pragma mark - 确认
-(void)updatePassword{
    UITextField *text = (UITextField *)[self.view viewWithTag:5];
    NSString *udid = [[NSUserDefaults standardUserDefaults] objectForKey:@"UDIDStr"];
    
    if (writePhoneNum1.text == nil ||writePhoneNum1.text.length<=0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"请输入账号" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    if (writePhoneNum1.text == nil ||writePhoneNum1.text.length<=0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"请输入验证码" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    LoginData *login = [[LoginData alloc]init];
    [login verifyMessageCode:verifyStr andUserCode:text.text andServiceID:udid];
    login.delegate = self;
   
}
#pragma mark - 验证验证码回调
-(void)verifyMessage:(NSDictionary *)sign{
    if ([[sign objectForKey:@"status"] isEqualToString:@"success"]) {
    UpdatePasswordViewController *update = [[UpdatePasswordViewController alloc]init];
    [self.navigationController pushViewController:update animated:YES];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[sign objectForKey:@"reason"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
}

#pragma mark - 返回
-(void)clickRightBtn{
    
}
#pragma mark - 返回上一个界面
-(void)clickBackBtn{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
